﻿using System.IO;

using NUnit.Framework;
using Ninject;

using System.Net.DnsToolkit.Bastard.PacketGenerator;
using System.Net.DnsToolkit.Configuration;
using System.Net.DnsToolkit.Packet;
using System.Net.DnsToolkit.Serializer;
using System.Net.DnsToolkit.Util;
using System.Net.DnsToolkit.TestUnit.Helper;

namespace System.Net.DnsToolkit.TestUnit.Bastard.PacketGenerator {
    [TestFixture]
    public class DomainPointerOutOfRangeTest {

        [Test]
        public void TestGetBytes() {

            var generator = new DomainPointerOutOfRange();
            var bs = generator.GetBytes();
            var result = BitStringHelper.ByteArrayToString(bs);

            Assert.AreEqual("0000000000020000000000000130096C6F63616C686F7374086C6F63616C6E657400000100010131C0FF00010001", result, "result");
            Assert.AreEqual(0xFF, bs[41], "invalid pointer");

        }

        [Test]
        public void TestReadMessage() {

            var generator = new DomainPointerOutOfRange();
            var bs = generator.GetBytes();

            var message = DnsReader.Read(new MemoryStream(bs));
            // DarkDns toolkit is immune to OutOfRangeDomainPointer attack
            Assert.AreEqual(2, message.Questions.Count, "count");
            Assert.AreEqual("0.localhost.localnet.", message.Questions[0].QName.Name, "question0");
            Assert.AreEqual("1.", message.Questions[1].QName.Name, "question1");

        }

    }
}
